package com.base.cn.platform.os.service.op.coupon;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.dao.op.coupon.OpCouponShopDao;
import com.base.cn.platform.os.entity.op.coupon.CouponShopData;
import com.base.cn.platform.os.entity.op.coupon.OpCoupon;
import com.base.cn.platform.os.entity.op.coupon.OpCouponShop;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.service.order.init.ShopDataInit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 优惠券限制可使用的商品Biz
 *
 * @author s.li
 * @date 2019/05/14
 */
@Service
public class OpCouponShopBiz extends BaseBiz<OpCouponShop, OpCouponShopDao> {

    @Autowired
    private ShopDataInit shopDataInit;

    /**
     * 保存优惠券限制使用的商品数据
     * @param coupon 优惠券对象
     */
    public void saveCouponShopData(OpCoupon coupon){
        CouponShopData shopData = coupon.getShopData();
        if(shopData!=null){
            String courseIds = shopData.getCourseIds();
            String examIds = shopData.getExamIds();
            String bookIds = shopData.getBookIds();
            String memberIds = shopData.getMemberIds();
            String qaUse = shopData.getQaUse();
            String attendUse = shopData.getAttendUse();
            if(StringUtils.isNotEmpty(courseIds)
                    || StringUtils.isNotEmpty(examIds)
                    || StringUtils.isNotEmpty(bookIds)
                    || StringUtils.isNotEmpty(memberIds)
                    || qaUse.equals("on")
                    || attendUse.equals("on")){
                List<OpCouponShop> couponShopList = new ArrayList<>();
                if(StringUtils.isNotEmpty(courseIds)){
                    genShop(couponShopList,coupon,ShopType.ORDER_SHOP_COURSE,courseIds);
                }
                if(StringUtils.isNotEmpty(examIds)){
                    genShop(couponShopList,coupon,ShopType.ORDER_SHOP_EXAM,examIds);
                }
                if(StringUtils.isNotEmpty(bookIds)){
                    genShop(couponShopList,coupon,ShopType.ORDER_SHOP_BOOK,bookIds);
                }
                if(StringUtils.isNotEmpty(memberIds)){
                    genShop(couponShopList,coupon,ShopType.ORDER_SHOP_MEMBER,memberIds);
                }
                if(qaUse.equals("on")){
                    OpCouponShop couponShop = new OpCouponShop();
                    couponShop.setCouponId(coupon.getId());
                    couponShop.setShopType(ShopType.ORDER_SHOP_QA);
                    couponShop.setShopId(BigDecimal.ZERO);
                    couponShopList.add(couponShop);
                }
                if(attendUse.equals("on")){
                    OpCouponShop couponShop = new OpCouponShop();
                    couponShop.setCouponId(coupon.getId());
                    couponShop.setShopType(ShopType.ORDER_SHOP_ATTEND);
                    couponShop.setShopId(BigDecimal.ZERO);
                    couponShopList.add(couponShop);
                }
                this.batchSave(couponShopList);
            }
        }
    }

    /**
     * 获取优惠券下的商品列表
     * @param couponId 优惠券的ID
     * @return List<OpCouponShop>
     */
    public List<OpCouponShop> findCouponShopList(BigDecimal couponId){
        List<OpCouponShop> couponShopList = this.find(" couponId = "+couponId,null,null);
        if(ObjectUtils.isNotEmpty(couponShopList)){
            List<ShopData> shopDataList = new ArrayList<>();
            couponShopList.forEach(c->{
                ShopData shop = new ShopData();
                shop.setShopId(c.getShopId());
                shop.setShopType(c.getShopType());
                shopDataList.add(shop);
            });
            shopDataInit.setShopData(shopDataList);
            if(ObjectUtils.isNotEmpty(shopDataList)){
                Map<String,ShopData> shopMap = shopDataList.stream().collect(Collectors.toMap(e->e.getShopType()+"_"+e.getShopId(), e->e));
                couponShopList.forEach(c->{
                    String key = c.getShopType()+"_"+c.getShopId();
                    ShopData shop = shopMap.get(key);
                    c.setShop(shop);
                });
            }
        }
        return couponShopList;
    }

    private void genShop(List<OpCouponShop> couponShopList,OpCoupon coupon,String shopType,String strShopId){
        String[] arr = StringUtils.subHeadTailString(strShopId,",").split(",");
        Arrays.stream(arr).forEach(str->{
            if(StringUtils.isNotEmpty(str)){
                OpCouponShop couponShop = new OpCouponShop();
                couponShop.setCouponId(coupon.getId());
                couponShop.setShopType(shopType);
                couponShop.setShopId(new BigDecimal(str));
                couponShopList.add(couponShop);
            }
        });
    }

}
